//
//  ViewController.m
//  TCProject
//
//  Created by admin on 16/1/18.
//  Copyright © 2016年 wise-wing. All rights reserved.
//

#import "ViewController.h"
#import "BannerView.h"
#import "AD.h"
#import "MenuView.h"
#import "AccountManager.h"
#import "LoginViewController.h"
#import "BaseNavController.h"
#import "BMBaseModel+BMExtensions.h"
#import "WaitOrderController.h"
#import "CZAccountSecureController.h"
#import "CZPersonInfoController.h"
#import "WebViewController.h"


@interface ViewController ()
@property (nonatomic, strong) MenuView *menuView;
@property (weak, nonatomic) IBOutlet BannerView *bannerView;
@property (strong, nonatomic) IBOutletCollection(UITapGestureRecognizer) NSArray *itemAction;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    weakSelf(self);
    self.menuView = [[MenuView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"CZmenu" withExtension:@"plist"];
    self.menuView.menuItems = [NSArray arrayWithContentsOfURL:url];
//    NSLog(@"%ld",self.menuView.menuItems.count);
    [self.menuView setMenuClicked:^(NSIndexPath *indexPath) {
        
        if (!indexPath) {
            CZPersonInfoController *personVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"personVC"];
            [weakSelf showViewController:personVC sender:nil];
            return;
        }
        
        BOOL row = indexPath.row;
        UIStoryboard *storyBoaard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *destVC;
        switch (indexPath.section) {
            case 0:
                switch (indexPath.row) {
                    case 0:
                        destVC = [storyBoaard instantiateViewControllerWithIdentifier:@"orderVC"];
                        break;
                    case 1:
                        destVC = [storyBoaard instantiateViewControllerWithIdentifier:@"carVC"];
                        break;
                    case 2:
                        destVC = [storyBoaard instantiateViewControllerWithIdentifier:@"incomeVC"];
                        break;
                }
                break;
            case 1:
                if (row) {
                    destVC = [storyBoaard instantiateViewControllerWithIdentifier:@"msgVC"];
                } else {
                    destVC = [[CZAccountSecureController alloc] init];
                }
                break;
            case 2:
                destVC = [storyBoaard instantiateViewControllerWithIdentifier:@"settingVC"];
                break;
        }
        if (destVC) {
            [weakSelf showViewController:destVC sender:nil];
        }
    }];
    
    [self.bannerView setItemClick:^(AD *ad) {
        WebViewController *webVC = [WebViewController new];
        webVC.path = [NSURL URLWithString:ad.linkSrc];
        NSLog(@"%@",webVC.path);
        [self showViewController:webVC sender:nil];
    }];

    [self getData];
    }

- (void)getData
{
    BMRequest *request = [BMRequest requestWithPath:@"findAdList.htm" contentKey:@"adverList"];
    request.params = @{@"position": @3};
    [AD postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        if (response.status == kResultOK) {
            self.bannerView.banners = response.result;
            [self.bannerView start];
        } else {
            [self showHint:@"获取广告列表失败"];
        }
        [self getMenuAD];
    }];
}

- (void)getMenuAD
{
    BMRequest *request = [BMRequest requestWithPath:@"findAdList.htm" contentKey:@"adverList"];
    request.params = @{@"position": @1};
    [AD postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        if (response.status == kResultOK) {
            self.menuView.bannerView.banners = response.result;
            [self.bannerView start];
        }
    }];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.bannerView start];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.bannerView stop];
    
}
- (IBAction)itemAction:(UITapGestureRecognizer *)sender {
    
    NSUInteger tag = sender.view.tag;
    UIViewController *destVC;
    BOOL isLogin = [[AccountManager sharedInstance] isLogin];
    switch (tag) {
        case 1:
            destVC = [self.storyboard instantiateViewControllerWithIdentifier:@"waitOrderVC"];
            break;
        case 2:
            if (isLogin) {
                destVC = [self.storyboard instantiateViewControllerWithIdentifier:@"orderVC"];
            } else {
                destVC = [self.storyboard instantiateViewControllerWithIdentifier:@"loginVC"];
            }
            break;
        case 3:
            if (isLogin) {
                destVC = [self.storyboard instantiateViewControllerWithIdentifier:@"persnoVC"];
            } else {
                destVC = [self.storyboard instantiateViewControllerWithIdentifier:@"loginVC"];
            }
            break;
        case 4:
            if (isLogin) {
                destVC = [self.storyboard instantiateViewControllerWithIdentifier:@"incomeVC"];
            } else {
                destVC = [self.storyboard instantiateViewControllerWithIdentifier:@"loginVC"];
            }
            break;
    }
    if ([destVC isKindOfClass:[BaseNavController class]]) {
        [self presentViewController:destVC animated:YES completion:nil];
    } else {
        [self showViewController:destVC sender:nil];
    }

    
}

- (IBAction)personInfo:(id)sender {
    if ([[AccountManager sharedInstance] isLogin]) {
        [_menuView show];
    } else {
        LoginViewController *loginVC = [self.storyboard instantiateViewControllerWithIdentifier:@"loginVC"];
        [self presentViewController:loginVC animated:YES completion:nil];
    }

}


@end
